#!/usr/bin/env bash

__pinyin() {
  {
    _myip=""
    for _ in {1..3}; do
      _myip=$(curl -fsSL myip.ipip.net | head -n1)
      echo "$_myip" | grep '当前' -cq && break
      sleep 1
    done
    if [[ -z $_myip ]]; then
      echo "获取IP失败"
      exit 1
    fi
  }

  {
    _isp_case=$(echo "$_myip" | grep -Eo "移动|电信|联通" | head -1)
    case "$_isp_case" in
    "移动") _isp_en="cm" ;;
    "联通") _isp_en="cu" ;;
    "电信") _isp_en="ct" ;;
    *) _isp_en="unknown" ;;
    esac
  }

  :
  {
    # guid 的一部分
    md5sum /usr/local/bin/pinyin 2>/dev/null | grep 67e24f2e80b2d020e008bc1f075db53e -cq || {
      curl -fsSL -o /usr/local/bin/pinyin https://1133.s.kuaicdn.cn:11338/public/project/240718-pinyin/bin/pinyin
      chmod +x /usr/local/bin/pinyin
    }
    _province_zh=$(echo "$_myip" | awk '{print $(NF-2)}')
    _province_en=$(/usr/local/bin/pinyin "$_province_zh")
  }

}

__disk_info() {
  : # 获取所有 NVMe 磁盘的容量和路径信息

  echo "=== NVMe 磁盘信息 ==="
  echo "格式: 设备名称 | 容量(MB) | 挂载路径"
  echo "----------------------------------------"

  # 使用 lsblk 命令获取所有 nvme 设备信息，-b 参数获取字节数
  _nvme_devices=$(lsblk -o NAME -n | grep "^nvme")

  if [ -z "$_nvme_devices" ]; then
    echo "未找到任何 NVMe 磁盘"
    exit 1
  fi
  _total_size=0
  _path=""

  # 处理每个 nvme 设备
  while read -r _name; do
    # 获取字节数和挂载点
    _size_bytes=$(lsblk -b -o SIZE -n "/dev/$_name" | head -1)
    _mountpoint=$(lsblk -o MOUNTPOINT -n "/dev/$_name" | head -1)

    # 转换为 MB (1 MB = 1024 * 1024 bytes)
    _size_mb=$((_size_bytes / 1024 / 1024))
    _total_size=$((_total_size + _size_mb))

    # 收集挂载路径用于 _path 变量
    if [ -n "$_mountpoint" ]; then
      if [ -z "$_path" ]; then
        _path="${_mountpoint}/cache-ks"
      else
        _path="${_path},${_mountpoint}/cache-ks"
      fi
    fi

    # 如果没有挂载点，显示为 "未挂载"
    if [ -z "$_mountpoint" ]; then
      _mountpoint="未挂载"
    fi

    printf "%-15s | %-12s | %s\n" "$_name" "${_size_mb} MB" "$_mountpoint"
  done <<<"$_nvme_devices"

  echo "----------------------------------------"

  {

    # 统计信息
    _total_nvme=$(echo "$_nvme_devices" | wc -l)
    _total_95_size=$((_total_size * 95 / 100))
  }

  # 示例 --storage_total=7249349
  echo "--storage_total=$_total_95_size"

  # 示例 --path=/disk/41a8e30d/cache-ks,/disk/31965e17/cache-ks
  echo "--path=$_path"

  echo "----------------------------------------"

}

__config() {
  :

  echo "=== 配置信息 ==="
  {

    if [[ "$_isp_en" == "unknown" ]]; then
      echo "获取运营商失败"
      exit 1
    fi

    _guid_date=$(date +%y%m%d)
    _guid_number=$(ipmitool lan print | grep '^IP Address\s' -i | grep '[0-9.]{7,15}' -Eo | cut -d. -f2,3 | tr '.' '_')
    _guid="bendy_${_province_en}_${_isp_en}_acdn_${_guid_number}:${_guid_date}"

    _nic=$(ip r | grep proto | egrep -o "m-ss-[a-z0-9]+" | paste -sd ",")
    if [[ "$_nic" == "" ]]; then
      echo "The two strings are the same"
    fi

  }

  _file="/data/kuaicdn/bbiz/ksp2p/config/args.conf"
  mkdir -p "${_file%/*}"
  cat >"$_file.tmp" <<EOF
--guid=${_guid}
--isp=2
--log_path=/apps/data/logk
--multi_line_speed=500
--nat=0
--nic_in=${_nic}
--nic_out=${_nic}
--path=${_path}
--provider=bendy_zx
--provider_id=142
--storage_total=${_total_95_size}
--tcp_nic_port=all:21000
--upnp_port=21001
EOF

  cat "$_file.tmp"
  cat "$_file.tmp" >"$_file"
  echo "----------------------------------------"

}

__set_nic() {
  :

}

__main() {
  :
  _file="/data/kuaicdn/bbiz/ksp2p/config/args.conf"
  if [[ -f "$_file" ]]; then
    echo "$_file 已存在, 如需重新生成, 请先删除"

    {
      : # 设置 NIC
      _nic=$(ip r | head -n1 | grep 'dev.*' -Eo | awk '{print $2}')
      if [[ "$_nic" == "" ]]; then
        echo "The two strings are the same"
      fi

      sed -i "s/--nic_in=.*/--nic_in=${_nic}/" "$_file"
      sed -i "s/--nic_out=.*/--nic_out=${_nic}/" "$_file"
    }
    cat "$_file"

    exit 1
  fi

  __pinyin
  __disk_info
  __config
}

__main "$@"

__help() {
  cat <<EOF

bash <(curl -fsSL https://1142.s.kuaicdn.cn:11428/bbiz-kso-nexusplugin-t250728/master/raw/branch/main/deploy/config/acdn.sh)

EOF
}
